package com.binarysoft.service.common.hbase;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import common.framework.dsb.client.DefaultServiceFactory;
import common.framework.dsb.client.ServiceFactory;

public class Test {

	public static void main(String[] args) throws Exception {

		ServiceFactory serviceFactory = new DefaultServiceFactory();

		HBaseService hbaseService = serviceFactory.lookup("DSB/HBaseService", "localhost", 10001);

		String clusterName = "hbase-cluster-1";
		String tableName = "netcare_v6:ne_basic_perf_origin";

		String[] families = new String[] { "cpu", "memory", "disk" };

		java.util.Random random = new java.util.Random();// 定义随机类

		int result = random.nextInt(10);// 返回[0,10)集合中的整数，注意不包括10

		long start = System.currentTimeMillis();
		Table table = hbaseService.getHtable(clusterName, tableName);
		List<Put> puts = new ArrayList<Put>();

		for (int i = 0; i < 10000; i++) {

			String rowKey = String.valueOf(System.nanoTime());
			int familyIndex = random.nextInt(3);// 返回[0,3)集合中的整数，注意不包括3
			String familyName = families[familyIndex];
			String columnName = String.valueOf(random.nextInt(10000));
			String value = String.valueOf(random.nextFloat() + 10000);

			Put put = new Put(Bytes.toBytes(rowKey));
			put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));
			put.setDurability(Durability.SYNC_WAL);
			puts.add(put);

		}
		table.put(puts);
		table.close();
		long end = System.currentTimeMillis();

		System.out.println("Consume: " + (start - end) + " times.");

	}
}